package com.example.journalegocentrique;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.TimeZone;

import android.annotation.SuppressLint;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

@SuppressLint("SimpleDateFormat")
public class UtilityDB {

	static final SQLiteDatabase database = GetDataBase.getDb();

	public static String getDayFromId(int idDay) {
		int id = idDay;
		String queryDay = "SELECT * FROM tbDay WHERE _id= '"+id+"'";
		Cursor c = database.rawQuery(queryDay, null);
		String data = null;
		if (c.getCount() > 0) {
			while (c.moveToNext()) {
				data = c.getString(1);
			}
		}
		c.close();
		return data;
	}
	
	public static int getIdDayFromDate(String day) {
		String tDay = day;
		int idDay=-1;
		String queryDay = "SELECT * FROM tbDay WHERE day='"+tDay+"' LIMIT 1";
		Cursor c = database.rawQuery(queryDay, null);
		
		if (c.getCount() > 0) {
			while (c.moveToNext()) {
				idDay = c.getInt(0);
			}
		}
		c.close();
		return idDay;
	}
	
	public static int getIdDayFromIdEntry(int idEntry) {
		int idDay=-1;
		String queryDay = "SELECT * FROM tbEntry WHERE _id="+idEntry;
		Cursor c = database.rawQuery(queryDay, null);
		
		if (c.getCount() > 0) {
			while (c.moveToNext()) {
				idDay = c.getInt(4);
			}
		}
		c.close();
		return idDay;
	}
	
	public static int getIdEmoticonFromEmoticon(String emot) {
		int idEmot=-1;
		String queryDay = "SELECT * FROM tbMood WHERE mood = '"+emot+"'";
		Cursor c = database.rawQuery(queryDay, null);
		
		if (c.getCount() > 0) {
			while (c.moveToNext()) {
				idEmot = c.getInt(0);
			}
		}
		c.close();
		return idEmot;
	}
	
	public static String getEmoticonFromIdEmoticon(int idEmot) {
		String Emot=null;
		String queryDay = "SELECT * FROM tbMood WHERE _id = '"+idEmot+"'";
		Cursor c = database.rawQuery(queryDay, null);
		
		if (c.getCount() > 0) {
			while (c.moveToNext()) {
				Emot = c.getString(1);
			}
		}
		c.close();
		return Emot;
	}


	public static int createDay()
	{
		TimeZone tz = TimeZone.getDefault();
		Calendar calendario = GregorianCalendar.getInstance(tz);
		calendario.add(GregorianCalendar.MONTH, 1);	
		return -1;
	}
	
	public static int getLastIdEntry(){
		int idEntry=-1;
		String queryDay = "SELECT _id FROM tbEntry ORDER BY _id DESC limit 1";
		Cursor c = database.rawQuery(queryDay, null);
		
		if (c.getCount() > 0) {
			while (c.moveToNext()) {
				idEntry = c.getInt(0);
			}
		}
		c.close();
		return idEntry;
	}
	
	@SuppressLint("SimpleDateFormat")
	public static String getCurrentDate(){
		String date;
		Calendar c = Calendar.getInstance();
		SimpleDateFormat df = new SimpleDateFormat("dd-MM-yyyy");
		date = df.format(c.getTime());
		return date;
	}
	
	public static String getCurrentExtendedDate(){
		String date;
		Calendar c = Calendar.getInstance();
		SimpleDateFormat df = new SimpleDateFormat("dd MMM yyyy");
		date = df.format(c.getTime());
		return date;
	}
	
	public static String getCurrentHour(){
		String hour;
		Calendar c = Calendar.getInstance();
		SimpleDateFormat df = new SimpleDateFormat("HH:mm");
		hour = df.format(c.getTime());
		return hour;
	}
	
	public static String getExtendedDateFromDate(String date){
		String extendedDate;
		Date myDate = null;
		SimpleDateFormat cur = new SimpleDateFormat("dd-MM-yyyy");
		try {
			 myDate = cur.parse(date);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		SimpleDateFormat df = new SimpleDateFormat("dd MMM yyyy");
		extendedDate = df.format(myDate);
		return extendedDate;
	}
	
	public static String getPlaceFromIdEntry(int idEntry){
		String place = null;
		String queryDay = "SELECT * FROM tbEntry WHERE _id="+idEntry;
		Cursor c = database.rawQuery(queryDay, null);
		if (c.getCount() > 0) {
			while (c.moveToNext()) 
				place = c.getString(6);
		}
		c.close();
		return place;
	}

	public static boolean checkEditEntry(int idEntry, int hours){
		String queryDay = "SELECT * FROM tbEntry WHERE _id="+idEntry;
		Cursor c = database.rawQuery(queryDay, null);
		GregorianCalendar cal = new GregorianCalendar();
		Date strDate = null;
			if (c.getCount() > 0) {
				while (c.moveToNext()) {
					String time = c.getString(1);
					int idData = c.getInt(4);
					String data = UtilityDB.getDayFromId(idData);
					data = data.replace("-", "/");
					data = data + " " +time;					
					SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm");			
					try {
						strDate = sdf.parse(data);
						cal.setTime(strDate);
						cal.add(Calendar.HOUR_OF_DAY, hours);
					} catch (ParseException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					// se 
					Date dateEdit = cal.getTime();
					Date dateNow = new Date();
					 if (dateNow.after(dateEdit)){
						 c.close();
						 return false;
					 }
					 else{
						 c.close();
						 return true;
					 }
				}
			}
			c.close();
			return true;
		}
}
